<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="标题" prop="title">
        <el-input v-model="formData.title" placeholder="标题" />
      </el-form-item>
      <el-form-item label="链接" prop="url">
        <el-input v-model="formData.url" placeholder="跳转链接" />
      </el-form-item>
      <div v-if="formData.miniprogram">
        <el-form-item label="小程序appid" prop="miniprogram.appid">
          <el-input v-model="formData.miniprogram.appid" placeholder="小程序appid"/>
        </el-form-item>
        <el-form-item label="小程序路径" prop="miniprogram.pagePath">
          <el-input v-model="formData.miniprogram.pagePath" placeholder="小程序pagePath"/>
        </el-form-item>
      </div>
      <el-form-item label="appid" prop="appid">
        <el-input v-model="formData.appid" placeholder="请输入appid" />
      </el-form-item>
<!--      <el-form-item label="微信公众号ID" prop="accountId">-->
<!--        <el-input v-model="formData.accountId" placeholder="请输入微信公众号ID" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="公众号模板ID" prop="templateId">-->
<!--        <el-input v-model="formData.templateId" placeholder="请输入公众号模板ID" />-->
<!--      </el-form-item>-->
      <el-form-item label="模版名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入模版名称" />
      </el-form-item>
      <el-form-item label="是否有效" prop="status">
        <el-switch v-model="formData.status" placeholder="是否有效" :active-value="1" :inactive-value="0"/>
      </el-form-item>
      <div class="form-group-area">
        <el-form-item  class="form-group-title">消息填充数据，请对照模板内容填写</el-form-item>
        <el-form-item>
          <el-input type="textarea" disabled autosize v-model="formData.content" placeholder="模版"/>
        </el-form-item>
        <el-row v-for="(item,index) in formData.data" :key="item.name">
          <el-col :span="16">
            <el-form-item :label="item.name" :prop="'data.'+index+'.value'" :rules="[{required: true,message: '填充内容不得为空', trigger: 'blur' }]">
              <el-input type="textarea" autosize rows="1" v-model="item.value" placeholder="填充内容"  />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="颜色" >
              <el-input type="color" v-model="item.color" placeholder="颜色"/>
            </el-form-item>
          </el-col>
        </el-row>
      </div>
<!--      <el-form-item label="消息内容" prop="data">-->
<!--        <el-input v-model="formData.data" placeholder="请输入消息内容" />-->
<!--      </el-form-item>-->

<!--      <el-form-item label="小程序信息" prop="miniprogram">-->
<!--        <el-input v-model="formData.miniprogram" placeholder="请输入小程序信息" />-->
<!--      </el-form-item>-->

    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { MsgTemplateApi } from '@/api/mp/msgtemplate'

/** 模板消息 表单 */
defineOptions({ name: 'MsgTemplateForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  appid: undefined,
  accountId: undefined,
  templateId: undefined,
  name: undefined,
  title: undefined,
  content: undefined,
  data: undefined,
  url: undefined,
  miniprogram: {appid:'',pagePath:''},
  status: undefined,
})
const formRules = reactive({
  title: [
    { required: true, message: '标题不能为空', trigger: 'blur' }
  ],
  data: [
    { required: true, message: '内容不能为空', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '模版名称不能为空', trigger: 'blur' }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await MsgTemplateApi.getMsgTemplate(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value
    if (formType.value === 'create') {
      await MsgTemplateApi.createMsgTemplate(data)
      message.success(t('common.createSuccess'))
    } else {
      await MsgTemplateApi.updateMsgTemplate(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    appid: undefined,
    accountId: undefined,
    templateId: undefined,
    name: undefined,
    title: undefined,
    content: undefined,
    data: undefined,
    url: undefined,
    miniprogram: undefined,
    status: undefined,
  }
  formRef.value?.resetFields()
}
</script>
